package com.controller.index;

import java.util.List;

import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.model.Teacher;

public class UserController extends Controller
{
    public void index() 
    {
        render("/pages/index.jsp");
    }
    public void login() 
    {
    	String userNo=this.getPara("user");
		String pwd=this.getPara("password");
		String buttonlist=this.getPara("buttonlist");
		String sql=null;
		
		setSessionAttr("buttonlist",buttonlist);
		
		String validcode=this.getPara("validcode"); 
		
		String check= this.getSessionAttr("check");
		
		
		if(validcode!=null && validcode!=null && !check.equals(validcode.toLowerCase()))
		{
			setSessionAttr("error","验证码错误");
			redirect("/");
		}
		else if(buttonlist.equals("A"))
		{
/*			Page<User> userPages=User.dao.paginate(1, 1, "select *", "from student where student_no=?",userNo);
			setSessionAttr("user", userPages.getList());			
			
			List<User> student=getSessionAttr("user");*/
		
			sql="select * from student where student_no=?";
			Record student=Db.findFirst(sql,userNo);
			
			if(null==student)
			{
				setSessionAttr("error","用户名不存在");
				redirect("/");
			}
			else 
			{
				int stuNum=student.getInt("student_id");
				setSessionAttr("stuNum", stuNum);
			
				String student_no=student.getStr("student_no");			
				setSessionAttr("student_no", student_no);
				
				String student_name=student.getStr("student_name");
				setSessionAttr("student_name", student_name);
				
				String student_pwd=student.getStr("student_pwd");
				if(student_no.equals(userNo) && student_pwd.equals(pwd))
				{	
					
					redirect("/pages1/stu_0.jsp");
				}
				else
				{
					setSessionAttr("error","用户名或密码错误");
					redirect("/");
				}
			
				
			}
		}
		else if(buttonlist.equals("B"))
		{
			
			/*Page<Teacher> teacherPage=Teacher.dao.paginate(1, 1, "select *", "from teacher where teacher_no=?",userName);
			setSessionAttr("teacher", teacherPage.getList());
			
			List<Teacher> myTeacher=getSessionAttr("teacher");
			String teaNum=myTeacher.get(0).getStr("teacher_no");
			
			String teaName=myTeacher.get(0).getStr("teacher_name");
			
			String teaPwd=myTeacher.get(0).getStr("teacherPwd");
			setSessionAttr("teacher_name", teaName);*/
			
			Page<Teacher> teacherPage=Teacher.dao.paginate(1, 1, "select *", "from teacher where teacher_no=?",userNo);
			setSessionAttr("teacher", teacherPage.getList());
			
			List<Teacher> myTeacher=getSessionAttr("teacher");
			
/*			sql="select * from myTeacher where myTeacher_no=?";
			Record myTeacher=Db.findFirst(sql,userNo);
			setSessionAttr("myTeacher",myTeacher);*/
		
			if(myTeacher.size()==0)
			{
				setSessionAttr("error","用户名不存在");
				redirect("/");
			}
			else
			{
				String teaCatergorry=myTeacher.get(0).getStr("teacher_staff_category");
				String teaNum=myTeacher.get(0).getStr("teacher_no");
				int teaId=myTeacher.get(0).getInt("teacher_id");
				setSessionAttr("teacher_id", teaId);
				
				String teaName=myTeacher.get(0).getStr("teacher_name");
			
				String teaPwd=myTeacher.get(0).getStr("teacher_pwd");
				setSessionAttr("teacher_name", teaName);

				if("教学秘书".equals(teaCatergorry))
				{
					if(teaNum.equals(userNo) && teaPwd.equals(pwd))
					{
						redirect("/pages1/secretary_0.jsp");
					}
					else
					{
						setSessionAttr("error","用户名或密码错误");
						redirect("/");
					}
				}
				else
				{
					if(teaNum.equals(userNo) && teaPwd.equals(pwd))
					{
						redirect("/pages1/finalT1.jsp");
					}
					else
					{
						setSessionAttr("error","用户名或密码错误");
						redirect("/");
					}
				}
				
			}
		}
		else if(buttonlist.equals("C"))
		{
			setSessionAttr("error","用户名不存在");
		}
	}
}
